package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.ProductRecord;
import com.sdry.model.hpf.ProductionStatistics;
import com.sdry.model.hpf.ProductionStatisticsDetail;
import com.sdry.model.hpf.StationStatus;
import com.sdry.service.hpf.AllReportService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: AllReportController 
 * @Description:所有报表Controller
 * @Author hpf
 * @Date 2018年11月28日 下午2:36:12 
 * @version 1.0
 */
@Controller
@RequestMapping(value="/reportMana")
public class AllReportController {

	@Resource
	private AllReportService allReportService;
	
	/**
	 * 查询生产记录报表
	 * @param time 查询时间
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listPageProductRecord")
	public void listPageProductRecord(String time,Page page, HttpServletResponse response,HttpServletRequest request) throws Exception{
		String path = request.getSession().getServletContext().getRealPath("/");
		System.out.println(path);
		Map<String, Object> map = new HashMap<>();
		map.put("page", page.getPage());
		map.put("limit", page.getLimit());
		if (null == time || "".equals(time)) {
			map.put("beginTime", "");
			map.put("endTime", "");
		} else {
			String[] dateArr = time.split(" - ");
			map.put("beginTime", dateArr[0]);
			map.put("endTime", dateArr[1]);
		}
		int count = allReportService.countCutByTime(map);
		List<ProductRecord> productRecordList = allReportService.listAllCutByTime(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(productRecordList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 查询生产现况看板
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listAllProductStatusByTime")
	public void listAllProductStatusByTime(HttpServletResponse response) throws Exception{
		List<StationStatus> stationStatusList = allReportService.listAllProductStatusByTime();
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(stationStatusList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", 0);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 查询产量统计报表
	 * @param time 查询时间
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/selectOutputByTime")
	public void selectOutputByTime(String time, HttpServletResponse response) throws Exception{
		List<ProductionStatistics> productionStatistics = allReportService.selectOutputByTime(time);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(productionStatistics, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", 10);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 查询产量详情
	 * @param time 查询条件
	 * @param pnum 查询条件
	 * @param response
	 */
	@RequestMapping(value="/listAllPLDetail")
	public void listAllPLDetail(String time,String pnum,HttpServletResponse response){
		List<ProductionStatisticsDetail> plDetailList = allReportService.listPagePLDetailByTime(time, pnum);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(plDetailList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", 100);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 安卓查询产量统计报表
	 * @param time 查询时间
	 * @param pnum 产品编号
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/selectAndroidOutputByTime")
	public void selectAndroidOutputByTime(String time, String pnum, HttpServletResponse response) throws Exception{
		JSONObject jsonObject = new JSONObject();
		List<ProductionStatisticsDetail> plDetailList = allReportService.selectAndroidOutputByTime(time, pnum);
		JSONArray jsonArray = JSONArray.fromObject(plDetailList);
		try {
			if(plDetailList.size() > 0){
				jsonObject.put("state", 1);
				jsonObject.put("message", "查询成功");
				jsonObject.put("data", jsonArray);
			}else{
				jsonObject.put("state", 0);
				jsonObject.put("message", "查询失败");
				jsonObject.put("data", null);
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
